#include "protocols/Ftp.h"
#include "utilities/ReadablePacket.h"
#include "protocols/ProtocalCode.h"
#include "protocols/ProtocalPool.h"
#include "protocols/ShowableItem.h"
#include "utilities/utilities.h"

REGISTER_PROTOCOL(FtpProtocal)

int FtpHeader::commandIndex() const
{
	const QByteArray data = QByteArray::fromRawData((const char *)mData, getHeaderLength());

	return data.indexOf(" ");
}

QString FtpHeader::getCommandInfo() const
{
	if (-1 != commandIndex())
	{
		QByteArray data = QByteArray::fromRawData((const char *)mData, commandIndex());
		QString command(data);
// 		switch(command)
// 		{
// 		case "220":
// 			return QString("Service ready for new user (%1)").arg(command);
// 		case "331":
// 			return QString("User name okay , need password (%1)").arg(command);
// 		case "230":
// 			return QString("User logged in , proceed (%1)").arg(command);
// 		case "200":
// 			return QString("command okay (%1)").arg(command);
// 		case "150":
// 			return QString("File status okay; about to open data connection (%1)").arg(command);
// 		case "226":
// 			return QString("Closing data connection (%1)").arg(command);
// 		case "221":
// 			return QString("Service closing control connection (%1)").arg(command);
// 		}
		if ("220" == command)
		{
			return QString("Service ready for new user (%1)").arg(command);
		}else if ("331" == command)
		{
			return QString("User name okay , need password (%1)").arg(command);
		}else if ("230" == command)
		{
			return QString("User logged in , proceed (%1)").arg(command);
		}else if ("200" == command)
		{
			return QString("command okay (%1)").arg(command);
		}else if ("150" == command)
		{
			return QString("File status okay; about to open data connection (%1)").arg(command);
		}else if ("226" == command)
		{
			return QString("Closing data connection (%1)").arg(command);
		}else if ("221" == command)
		{
			return QString("Service closing control connection (%1)").arg(command);
		}else
		{
			return command;
		}
	}else
	{
		QByteArray data = QByteArray::fromRawData((const char *)mData, getHeaderLength() - 2);
		return QString(data);
	}
}

QString FtpHeader::getArgInfo() const
{
	QByteArray data = QByteArray::fromRawData((const char *)(mData + commandIndex()),  getHeaderLength() - commandIndex() - 3);
	return QString(data);
}

void FtpHeader::toShowableItems(ShowableItem* root) const
{	
	int pos = mPos;
	ShowableItem* tmp,* flgs;

	ShowableItem* proto = createProtocalItem("File Transfer Protocol (FTP)", pos, getHeaderLength());
	root->pushChild(proto);

	tmp = createItemUnchange(qPrintable(byteToString((const char *)mData,"\r\n")),"",QString(""),pos, getHeaderLength());
	proto->pushChild(tmp);

	flgs = createItem("Request command: ", getCommandInfo(), pos, commandIndex());
	tmp->pushChild(flgs);

	flgs = createItem("Request arg: " ,getArgInfo(), pos, getHeaderLength() - commandIndex() - 3);
	tmp->pushChild(flgs);
}

FtpProtocal::FtpProtocal() : Protocol(FTP,QString("FTP"))
{
	shared_ptr<AppProtocolProperty> prop = make_shared<AppProtocolProperty>();
	prop->mDefaultPort.push_back(21);
	mInfo = prop;
}

PacketHeader*
FtpProtocal::analyzeProc(ReadablePacket*packet,int low,int high,Protocol*& tip)
{
	FtpHeader* header = new FtpHeader(FtpProtocal_register.m_Instance,packet->getData(),low,high - low + 1);

	tip = NULL;

	return header;
}

QString FtpHeader::getSummary() const{
	QByteArray data = QByteArray::fromRawData((const char *)mData,getHeaderLength());
	return QString((const char *) mData);
}